//
//  YSSelectQuizView.m
//  Yaoyle
//
//  Created by ZhaoXudong on 14-9-26.
//  Copyright (c) 2014年 ZhaoXudong. All rights reserved.
//

#import "YSSelectQuizView.h"
#import "UIViewExt.h"
#import "YSUi.h"

static CGFloat btnSpace = 6;

static CGFloat numberBtnSize = 24;

@interface YSSelectQuizView () {
    UIScrollView *scrollView;
    UIImageView *shaiBg;
    
    NSInteger upValue;
    NSInteger downValue;
    
    NSInteger minSelectNumber;
    
    UILabel *preUserPoint;
    UIImageView *prePointView;
}

@end

@implementation YSSelectQuizView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.backgroundColor = [UIColor blackColor];
        CGFloat leftSpace = 10;
        CGFloat moveBtnSize = 28;
        NSInteger shaiSize = 26;
        
        UIFont *font = [UIFont boldSystemFontOfSize:13];
        NSString *text = @"上家叫骰: 5";
        CGSize textSize = [text sizeWithFont:font];
        
        preUserPoint = [[UILabel alloc] initWithFrame:CGRectMake(leftSpace, 10, textSize.width, shaiSize)];
        [preUserPoint setBackgroundColor:[UIColor clearColor]];
        preUserPoint.text = text;
        preUserPoint.textColor = [UIColor whiteColor];
        preUserPoint.font = font;
        preUserPoint.hidden = YES;
        [self addSubview:preUserPoint];
        
        prePointView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bs_6"]];
        prePointView.frame = CGRectMake(preUserPoint.right + 6, 10, shaiSize, shaiSize);
        prePointView.hidden = YES;
        [self addSubview:prePointView];
        
        UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [leftBtn setImage:[UIImage imageNamed:@"left_move"]
                 forState:UIControlStateNormal];
        [leftBtn setImage:[UIImage imageNamed:@"left_move_gray"]
                 forState:UIControlStateHighlighted];
        [leftBtn addTarget:self
                    action:@selector(leftMove)
          forControlEvents:UIControlEventTouchUpInside];
        [leftBtn setFrame:CGRectMake(leftSpace,
                                     50,
                                     moveBtnSize,
                                     moveBtnSize)];
        leftBtn.tag = 100;
        [self addSubview:leftBtn];
        
        // 5
        scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(leftBtn.right,
                                                                    50,
                                                                    (numberBtnSize + btnSpace) * 5,
                                                                    numberBtnSize)];
        
        scrollView.contentSize = CGSizeMake((numberBtnSize + btnSpace) * 23, numberBtnSize);
        
        for (int nIndex = 1; nIndex < 24; nIndex++) {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.tag = nIndex + 1;
            [btn setFrame:CGRectMake(btnSpace + (numberBtnSize + btnSpace) * (nIndex - 1),
                                     0, numberBtnSize, numberBtnSize)];
            [btn setTitle:[NSString stringWithFormat:@"%d",nIndex + 1] forState:UIControlStateNormal];
            [btn setBackgroundImage:[UIImage imageNamed:@"btn_chai"] forState:UIControlStateNormal];
            [btn setBackgroundImage:[UIImage imageNamed:@"btn_chai_gray"] forState:UIControlStateHighlighted];
            [btn addTarget:self action:@selector(upBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            [scrollView addSubview:btn];
        }
        [self addSubview:scrollView];
        
        UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [rightBtn setImage:[UIImage imageNamed:@"right_move"]
                  forState:UIControlStateNormal];
        [rightBtn setImage:[UIImage imageNamed:@"right_move_gray"]
                  forState:UIControlStateHighlighted];
        [rightBtn addTarget:self
                     action:@selector(rightMove)
           forControlEvents:UIControlEventTouchUpInside];
        [rightBtn setFrame:CGRectMake(self.frame.size.width - leftSpace - moveBtnSize,//scrollView.right + btnSpace,
                                      50,
                                      moveBtnSize,
                                      moveBtnSize)];
        rightBtn.tag = 101;
        [self addSubview:rightBtn];
        
        // 下面的骰子
        shaiBg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"shai_view_bg"]];
        shaiBg.frame = CGRectMake(leftSpace, scrollView.bottom + 10, self.frame.size.width - leftSpace * 2, shaiSize + 10);
        [self addSubview:shaiBg];
        
        NSInteger shaiSpace = (shaiBg.frame.size.width - (shaiSize * 6 + leftSpace * 2)) / 5;
        
        for (int nIndex = 1; nIndex <= 6; nIndex ++) {
            NSString *nImageName = [NSString stringWithFormat:@"bs_%d",nIndex];
            NSString *hImageName = [NSString stringWithFormat:@"bs%d_gray",nIndex];
            
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.tag = nIndex;
            [btn setFrame:CGRectMake(shaiBg.left + leftSpace + (shaiSize + shaiSpace) * (nIndex - 1),
                                     shaiBg.top + 5,
                                     shaiSize,
                                     shaiSize)];
            [btn setImage:[UIImage imageNamed:nImageName] forState:UIControlStateNormal];
            [btn setImage:[UIImage imageNamed:hImageName] forState:UIControlStateHighlighted];
            
            [btn addTarget:self action:@selector(downBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:btn];
        }
    }
    return self;
}

- (void)rightMove {
    CGPoint point = scrollView.contentOffset;
    CGFloat offset = point.x + 3 * (numberBtnSize + btnSpace);
    if (offset >= 540) {
        offset = 540;
    }
    
    [scrollView setContentOffset:CGPointMake(offset, point.y) animated:YES];
}

- (void)leftMove {
    CGPoint point = scrollView.contentOffset;

    CGFloat offset = point.x - 3 * (numberBtnSize + btnSpace);
    if (offset <= 0) {
        offset = 0;
    }
    
    [scrollView setContentOffset:CGPointMake(offset, point.y) animated:YES];
    
}

- (void)upBtnClick:(id)sender {
    
    for (UIView *v in scrollView.subviews) {
        if ([v isKindOfClass:[UIButton class]]) {
            UIButton *button = (UIButton *)v;
            [button setBackgroundImage:[UIImage imageNamed:@"btn_chai"] forState:UIControlStateNormal];
        }
    }
    
    UIButton *btn = (UIButton *)sender;
    NSInteger tag = btn.tag;
    
    [btn setBackgroundImage:[UIImage imageNamed:@"btn_chai_gray"] forState:UIControlStateNormal];
    //    if (tag > minSelectNumber) {
    upValue = tag;
    if (downValue != 0) {
        if (_block) {
            _block(upValue, downValue);
        }
    }
    //    }
}

- (void)downBtnClick:(id)sender {
    
    for (UIView *v in self.subviews) {
        if ([v isKindOfClass:[UIButton class]]) {
            UIButton *button = (UIButton *)v;
            if (button.tag < 100) {
                NSString *imageName = [NSString stringWithFormat:@"bs_%d",button.tag];
                [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
            }
        }
    }
    
    UIButton *btn = (UIButton *)sender;
    NSInteger tag = btn.tag;
    NSString *imageName = [NSString stringWithFormat:@"bs%d_gray",tag];
    [btn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    
    downValue = tag;
    if (upValue != 0) {
        if (_block) {
            _block(upValue, downValue);
        }
    }
}

- (void)clearValue {
    for (UIView *v in self.subviews) {
        if ([v isKindOfClass:[UIButton class]]) {
            UIButton *button = (UIButton *)v;
            if (button.tag < 100) {
                NSString *imageName = [NSString stringWithFormat:@"bs_%d",button.tag];
                [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
            }
        }
    }
    
    for (UIView *v in scrollView.subviews) {
        if ([v isKindOfClass:[UIButton class]]) {
            UIButton *button = (UIButton *)v;
            [button setBackgroundImage:[UIImage imageNamed:@"btn_chai"] forState:UIControlStateNormal];
        }
    }
    
    upValue = 0;
    downValue = 0;
}


- (void)setMinSelectNum:(NSInteger)minNum {
    minSelectNumber = minNum;
}

- (void)setPrePointInfo:(NSInteger)dicePoint diceNum:(NSInteger)num {
    
    NSString *text = [NSString stringWithFormat:@"上家叫骰: %d",num];
    preUserPoint.text = text;
    preUserPoint.hidden = NO;
    
    NSString *imageName = [NSString stringWithFormat:@"bs_%d",dicePoint];
    prePointView.image = [UIImage imageNamed:imageName];
    prePointView.hidden = NO;
    [self addSubview:prePointView];

}
@end
